package Game;
import java.util.*;
public class Cancel {
/**
 * 
 * 
 */
	public static ArrayList<Button>buttons;
	public static void CancelOnce(ArrayList<Button> Buttons){
		buttons=Buttons;
		new Thread(){
			public void run(){
				try{
					do {
				int firstLine=buttons.get(0).setX;
				int lastLine=buttons.get(buttons.size()-1).setX;
				// 每一列要消的个数
				ArrayList<Integer> store=new ArrayList<Integer>();
				//每一列图片对应数字
				ArrayList<String> linetemp=new ArrayList<String>();
				//有可消的列的第一个可消的所对应的button数组中的位置
				ArrayList<Integer> linefirst=new ArrayList<Integer>(); 
				//这一列是否有可以消的
				ArrayList<Boolean> exist=new ArrayList<Boolean>();
				
					
					for(Button b:buttons){
						switch(b.PictureNum){
						case 1:b.setIcon(Pictures.Special1);break;
						case 2:b.setIcon(Pictures.Special2);break;
						case 3:b.setIcon(Pictures.Special3);break;
						case 4:b.setIcon(Pictures.Special4);break;
						case 5:b.setIcon(Pictures.Special5);break;
						case 6:b.setIcon(Pictures.Special6);break;
						}
					}
				
					Thread.sleep(400);
		//遍历buttons可消数组，并且对应每列可消个数以及第一个对应位置		
		for(int i=0;i<buttons.size();i++){
			int count=1;
			linefirst.add(i);
			int x=buttons.get(i).setX;
			if(x==lastLine){
				count=buttons.size()-i;
				store.add(count);
				break;
			}else{
			while(buttons.get(i+1).setX==x){
				count++;
				i++;
			}
			store.add(count);
			}
		}
		//确认每一列是否有可以消的
		for(int i=0;i<=lastLine-firstLine;i++){
			boolean tempExist=false;
			for(Button b:buttons){
				if(b.setX==i+firstLine){
					tempExist=true;
					break;
				}
			}
			exist.add(tempExist);
		}
		//使每个数组长度相同方便查询
		for(int i=0;i<=lastLine-firstLine;i++){
			if(!exist.get(i)){
				store.add(i,0);
				linefirst.add(i,-1);
			}
		}
		if(firstLine==lastLine){
			//linetemp只存储一列中可消最后一个的上面所有
			for(int a=0;a<=buttons.get(buttons.size()-1).setY;a++){
				linetemp.add(Block.Buttons[a][firstLine].PictureNum+"");
			}
			for(int z=0;z<buttons.size();z++){
				linetemp.remove(buttons.get(0).setY);
			}
			for(int b=0;b<buttons.size();b++){
				linetemp.add(0, ((int)(Math.random()*6+1))+"");
			}
			for(int c=0;c<=buttons.get(buttons.size()-1).setY;c++){
				Block.Buttons[c][firstLine].PictureNum=Integer.parseInt(linetemp.get(c));
			}
			for(int i=0;i<linetemp.size();i++){
				int pictureNum=Block.Buttons[i][firstLine].PictureNum;
				switch(pictureNum){
				case 1:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon1);
					break;
				case 2:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon2);
					break;
				case 3:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon3);
					break;
				case 4:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon4);
					break;
				case 5:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon5);
					break;
				case 6:
					Block.Buttons[i][firstLine].setIcon(Pictures.icon6);
					break;
				}
			}
			linetemp.clear();
			
		}else{
		for(int i=0;i<=lastLine-firstLine;i++){
			if(exist.get(i)){
			for(int j=0;j<=buttons.get(linefirst.get(i)+store.get(i)-1).setY;j++){
				linetemp.add(Block.Buttons[j][i+firstLine].PictureNum+"");
			}
		    
			for(int k=0;k<store.get(i);k++){
				linetemp.remove(buttons.get(linefirst.get(i)+k).setY-k);
			}
			for(int n=0;n<store.get(i);n++){
				linetemp.add(0, ((int)(Math.random()*6+1))+"");
			}
			for(int l=0;l<=buttons.get(linefirst.get(i)+store.get(i)-1).setY;l++){
				Block.Buttons[l][i+firstLine].PictureNum=Integer.parseInt(linetemp.get(l));
			}
			for(int m=0;m<linetemp.size();m++){
				int pictureNum=Block.Buttons[m][i+firstLine].PictureNum;
				switch(pictureNum){
				case 1:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon1);
					break;
				case 2:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon2);
					break;
				case 3:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon3);
					break;
				case 4:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon4);
					break;
				case 5:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon5);
					break;
				case 6:
					Block.Buttons[m][i+firstLine].setIcon(Pictures.icon6);
					break;
				}
			}
			linetemp.clear();
		}
		}
		}		Thread.sleep(400);
		AutoCancel.autoCancel();
					}while(AutoCancel.judge());

			}catch(InterruptedException e){
				e.printStackTrace();
			}
						}
		}.start();
	}
}